package com.kongzue.baseokhttp.util;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: @Kongzue
 * Github: https://github.com/kongzue/
 * Homepage: http://kongzue.com/
 * Mail: myzcxhh@live.cn
 * CreateTime: 2018/12/5 17:48
 */
public class BaseOkHttp {

    /**
     * 普通POST
     */
    public static final int POST_REQUEST = 0;

    /**
     * 普通GET
     */
    public static final int GET_REQUEST = 1;
    /**
     * 普通PUT
     */
    public static final int PUT_REQUEST = 2;
    /**
     * 普通DELETE
     */
    public static final int DELETE_REQUEST = 3;
    /**
     * 下载
     */
    public static final int DOWNLOAD = 4;

    public static void cleanSameRequestList() {
        requestInfoList = new ArrayList<>();
    }

    private static List<RequestInfo> requestInfoList;

    protected void addRequestInfo(RequestInfo requestInfo) {
        synchronized (BaseOkHttp.class) {
            if (requestInfoList == null) {
                requestInfoList = new ArrayList<>();
            }
            if (BaseOkHttpConfig.getInstance().isDebugMode()) LockLog.logI(">>>", "addRequestInfo: " + requestInfo);
            requestInfoList.add(requestInfo);
        }
    }

    protected void deleteRequestInfo(RequestInfo requestInfo) {
        synchronized (BaseOkHttp.class) {
            if (requestInfoList == null || requestInfoList.isEmpty() || requestInfo == null) {
                return;
            }
            requestInfoList.remove(requestInfo);
        }
    }

    protected boolean equalsRequestInfo(RequestInfo requestInfo) {
        synchronized (BaseOkHttp.class) {
            if (requestInfoList == null || requestInfoList.isEmpty()) {
                if (BaseOkHttpConfig.getInstance().isDebugMode()) LockLog.logI(">>>", "requestInfoList: null");
                return false;
            }
            for (RequestInfo requestInfo1 : requestInfoList) {
                if (BaseOkHttpConfig.getInstance().isDebugMode())
                    LockLog.logI(">>>", "equalsRequestInfo: " + requestInfo1);
                if (requestInfo1.equals(requestInfo)) {
                    if (BaseOkHttpConfig.getInstance().isDebugMode()) {
                        LockLog.logI(">>>", "发生重复请求");
                    }
                    return true;
                }
            }
            return false;
        }
    }
}
